<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cookie;


class role extends Login
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($page = 1)
    {
        $count = Db::name('role')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        //查询gqx_admin,role,user_role表
        $data = Db::name('role')->order(['id'=>'asc'])->page($page,10)->select();
        // return($data);
        return view('role/index',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data,
            'count'=>$count,
            'page'=>$page,
            'admin_name'=>Session::get('admin_name'),
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    public function search($page = 1,Request $request)
    {
        $post = $request->post();
        $count = Db::name('role')->where('role','like','%'.$post['like'].'%')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('role')->order(['id'=>'asc'])->where('role','like','%'.$post['like'].'%')->page($page,10)->select();
        // return($data);
        return view('role/index',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data,
            'count'=>$count,
            'page'=>$page,
            'admin_name'=>Session::get('admin_name'),
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = Db::name('access')->select();
        // return dump($data);
        return view('role/add',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        // return $request->param()['pass'];
        // return $request->param();
        
        if(@$request->param()['status'] == 'on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $data['role'] = $request->param()['role'];
        $data['create_time'] = date('Y-m-d H:i:s');
        // return($data);
        $s = Db::name('role')->insert($data);
        if(!$s){
            return '添加失败';
        }

        // 判断是否提交对应权限
        if(@empty($request->param()['access'])){
            return '添加角色成功，未赋予权限';
        }

        $userId = Db::name('user')->getLastInsID();
        $access = $request->param()['access'];
        foreach ($access as $k => $v) {
            $arr[] = ['role_id'=>$userId,'access_id'=>$v];
        }
        // return(dump(json_encode($arr)));
        $n = Db::name('role_access')->insertAll($arr);

        if($n > 0){
            return '添加成功';
        }
        return '权限添加失败，请稍后再试';

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        // return $id;
        $data = Db::name('role')->where('id',$id)->find();
        $access = Db::name('access')->select();
        $arr = Db::name('role_access')->where('role_id',$id)->select();
        // var_dump($arr);
        // return var_dump($data,$access);
        $list = '';
        foreach ($arr as $v) {
            $list .= ','.$v['access_id'].',';
        }
        // return $list;
        return view('role/edit',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'access'=>$access,
            'data'=>$data,
            'list'=>$list
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        //return $request->param();
        // 直接处理状态更改请求
        // return(var_dump($request->param(),$id));
        if(@$request->param()['source'] == 'status'){
            $data['status'] = $request->param()['status'];
            return Db::name('role')->where('id',$id)->update($data);
        }

        // 处理编辑
        if(@$request->param()['status'] == 'on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $data['role'] = $request->param()['role'];
        // return($data);
        $s = Db::name('role')->where('id',$id)->update($data);
        $str = $s ? '角色信息更新成功；' : '角色信息未更改；';

        Db::name('role_access')->where('role_id',$id)->delete();
        $access = $request->param()['access'];
        foreach ($access as $k => $v) {
            $arr[] = ['role_id'=>$id,'access_id'=>$v];
        }
        // return(dump(json_encode($arr)));
        $n = Db::name('role_access')->insertAll($arr);
        $str .= '对应权限数据更新'.$n.'条';
        return $str;
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        return Db::name('role')->where('id',$id)->delete();
    }
}
